package handler

//
//import (
//	"context"
//	"fmt"
//	"google.golang.org/grpc/codes"
//	"google.golang.org/grpc/status"
//	"google.golang.org/protobuf/types/known/emptypb"
//	"kuangstudy-mall/srvs/order-srv/global"
//	"kuangstudy-mall/srvs/order-srv/model"
//	"kuangstudy-mall/srvs/order-srv/proto"
//	"math/rand"
//	"time"
//)
//
//type OrderServer struct {
//}
//
//// 查询用户购物车列表
//func (handler *OrderServer) FindCartListByUserId(ctx context.Context, req *proto.CartUserInfo) (*proto.CartGoodListResponse, error) {
//	// 定义一个购物车的列表
//	var shoppingCarts []model.ShoppingCart
//	var response proto.CartGoodListResponse
//	result := global.DB.Where(&model.ShoppingCart{UserId: req.UserId}).Find(&shoppingCarts)
//	if result.Error != nil {
//		return nil, result.Error
//	}
//	// 返回购物车的总数量
//	response.Total = int32(result.RowsAffected)
//	// 返回购物车信息
//	for _, shopCart := range shoppingCarts {
//		response.GoodsList = append(response.GoodsList, &proto.ShopcartItemsResponse{
//			Id:      shopCart.ID,
//			UserId:  shopCart.UserId,
//			GoodsId: shopCart.GoodsId,
//			Nums:    shopCart.Nums,
//			Checked: shopCart.Checked,
//		})
//	}
//
//	return &response, nil
//}
//
//// 添加购物车
//func (handler *OrderServer) AddGoodsToShoppingCart(ctx context.Context, req *proto.CartItemRequest) (*proto.ShopcartItemsResponse, error) {
//	var shopCart model.ShoppingCart
//	if result := global.DB.Where(&model.ShoppingCart{GoodsId: req.GoodsId, UserId: req.UserId}).Find(&shopCart); result.RowsAffected > 0 {
//		// 这里传递的req.nums=1或者-1
//		shopCart.Nums += req.Nums
//	} else {
//		shopCart.UserId = req.UserId
//		shopCart.GoodsId = req.GoodsId
//		shopCart.Nums = req.Nums
//		shopCart.Checked = req.Checked
//	}
//	//save 方法 有id就更新，每页就保存
//	global.DB.Save(&shopCart)
//
//	return &proto.ShopcartItemsResponse{Id: shopCart.ID}, nil
//}
//
//// 更新购物车的状态
//func (handler *OrderServer) UpdateShoppingCartStatus(ctx context.Context, req *proto.CartItemRequest) (*emptypb.Empty, error) {
//	var shopCart model.ShoppingCart
//	if result := global.DB.Where(&model.ShoppingCart{GoodsId: req.GoodsId, UserId: req.UserId}).Find(&shopCart); result.RowsAffected == 0 {
//		return nil, status.Errorf(codes.NotFound, "购物车每页此商品...")
//	}
//	shopCart.Checked = req.Checked
//	if req.Nums > 0 {
//		shopCart.Nums = req.Nums
//	} else {
//		shopCart.Nums = 1
//	}
//	global.DB.Save(&shopCart)
//	return &emptypb.Empty{}, nil
//}
//
//// 删除多个
//func (handler *OrderServer) DeleteShoppingCartItems(ctx context.Context, req *proto.CartItemsRequest) (*emptypb.Empty, error) {
//	global.DB.Unscoped().Delete(&model.ShoppingCart{}, req.Ids)
//	return &emptypb.Empty{}, nil
//}
//
//// 删除单个
//func (handler *OrderServer) DeleteShoppingCartItem(ctx context.Context, req *proto.CartItemRequest) (*emptypb.Empty, error) {
//	global.DB.Unscoped().Delete(&model.ShoppingCart{}, req.Id)
//	return &emptypb.Empty{}, nil
//}
//
//// 创建订单
//func (handler *OrderServer) CreateGoodsOrder(ctx context.Context, req *proto.OrderRequest) (*proto.OrderInfoResponse, error) {
//	/**
//	 下单：
//		 1：查询购物车中用户选中的商品
//		 2：把选中的商品进行价格汇总----访问商品服务---远程服务调用
//		 3：库存服务扣减 ---访问库存服务---远程服务调用
//		 4: 保存订单的基础信息表
//		 5: 保存订单的明细表
//		 6: 开启消息队列队形订单的定时归还和修改状态
//		 本地事务--分布式事务
//	*/
//
//	// 1：查询购物车中用户选中的商品
//	var shoppingCarts []model.ShoppingCart
//	var goodsIds []uint64
//	if result := global.DB.Where(&model.ShoppingCart{UserId: req.UserId, Checked: true}).Find(&shoppingCarts); result.RowsAffected == 0 {
//		return nil, status.Errorf(codes.InvalidArgument, "购物车中没有结算的商品！")
//	}
//
//	// 这个map是用于后续的计算，解决双重for循环问题
//	shoppingCartsMap := map[uint64]int32{}
//	var shoppingCartsIds []uint64
//	for _, cart := range shoppingCarts {
//		goodsIds = append(goodsIds, cart.GoodsId)
//		shoppingCartsIds = append(shoppingCartsIds, cart.ID)
//		shoppingCartsMap[cart.GoodsId] = cart.Nums
//	}
//
//	// 2：把选中的商品进行价格汇总----访问商品服务---远程服务调用
//	batchGoodsInfo := proto.BatchGoodsInfo{Ids: goodsIds}
//	goodsListResponse, err := GetNacosGoodsClient().BatchGetGoodsList(ctx, &batchGoodsInfo)
//	if err != nil {
//		return nil, err
//	}
//
//	// 获取结算的商品价格
//	var totalPrice float32
//	var goodsStocksInfos []*proto.GoodsStocksInfo
//	var orderGoodsInfos []*model.OrderGoodsInfo
//	for _, goods := range goodsListResponse.Data {
//		// 计算支付总价
//		totalPrice += goods.SalePrice * float32(shoppingCartsMap[goods.Id])
//		// 扣减库存使用
//		goodsStocksInfos = append(goodsStocksInfos, &proto.GoodsStocksInfo{
//			GoodsId: goods.Id,
//			Num:     shoppingCartsMap[goods.Id],
//		})
//		// 准备订单明细和商品的数据
//		orderGoodsInfos = append(orderGoodsInfos, &model.OrderGoodsInfo{
//			OrderId:    0,
//			BaseModel:  model.BaseModel{Status: 1, IsDeleted: 0},
//			GoodsId:    goods.Id,
//			GoodsName:  goods.Name,
//			GoodsPrice: goods.SalePrice,
//			GoodsImage: goods.Images,
//			GoodsDesc:  goods.Desc,
//			Nums:       shoppingCartsMap[goods.Id],
//		})
//	}
//
//	// 3：库存服务扣减 ---访问库存服务---远程服务调用
//	var response proto.GoodsSellListRequest
//	response.GoodsList = goodsStocksInfos
//	if _, err := GetNacosGoodsStockClient().GoodsStocksSell(context.Background(), &response); err != nil {
//		return nil, status.Errorf(codes.ResourceExhausted, "扣减库存失败!")
//	}
//
//	tx := global.DB.Begin()
//
//	// 4: 保存订单
//	orderInfo := model.OrderInfo{
//		OrderNo:     CreateOrderSn(req.UserId),
//		OrderStatus: "PAYING",
//		OrderPrice:  totalPrice,
//		PayType:     0,
//		PayTime:     time.Now(),
//		UserId:      req.UserId,
//		Address:     req.Address,
//		SingerName:  req.Username,
//		SingerPhone: req.Mobile,
//		Post:        req.Post,
//	}
//
//	if result := tx.Save(&orderInfo); result.RowsAffected == 0 {
//		tx.Rollback()
//		return nil, status.Errorf(codes.Internal, "创建订单失败!")
//	}
//
//	// 订单和订单明细产生管理，就是绑定orderId
//	for _, orderGoods := range orderGoodsInfos {
//		orderGoods.OrderId = orderInfo.ID
//	}
//
//	// 5: 保存订单明细
//	tx.CreateInBatches(orderGoodsInfos, 100)
//	if result := tx.CreateInBatches(orderGoodsInfos, 100); result.RowsAffected == 0 {
//		tx.Rollback()
//		return nil, status.Errorf(codes.Internal, "批量插入订单商品失败!")
//	}
//
//	// 6: 删除购物车订单信息--选中的商品
//	if result := tx.Unscoped().Delete(&model.ShoppingCart{}, shoppingCartsIds); result.RowsAffected == 0 {
//		tx.Rollback()
//		return nil, status.Errorf(codes.Internal, "删除购物车记录失败!")
//	}
//	// 7: 返回
//	return &proto.OrderInfoResponse{
//		Id:          orderInfo.ID,
//		UserId:      req.UserId,
//		OrderNo:     orderInfo.OrderNo,
//		PayType:     orderInfo.PayType,
//		OrderStatus: orderInfo.OrderStatus,
//		PayPrice:    orderInfo.OrderPrice,
//		Username:    req.Username,
//		Address:     req.Address,
//		Mobile:      req.Mobile,
//		Post:        req.Post,
//		AddTime:     orderInfo.PayTime.Format("2006-01-02 15:04:05"),
//	}, nil
//}
//
//func CreateOrderSn(userId uint64) string {
//	now := time.Now()
//	rand.Seed(time.Now().UnixNano())
//	orderSn := fmt.Sprintf("%d%d%d%d%d%d%d%d",
//		now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Nanosecond(),
//		userId, rand.Intn(10)+10,
//	)
//	return orderSn
//}
//
//// 订单列表
//func (handler *OrderServer) FindGoodsOrderList(ctx context.Context, req *proto.OrderFilterRequest) (*proto.OrderListResponse, error) {
//	var orderList []model.OrderInfo
//	var response proto.OrderListResponse
//
//	// 1： 求总数
//	var total int64
//	global.DB.Where(&model.OrderInfo{UserId: req.UserId}).Count(&total)
//	response.Total = int32(total)
//	// 2: 查询分页订单信息
//	global.DB.Scopes(Paginate(int(req.PageNo), int(req.PageSize))).Where(&model.OrderInfo{UserId: req.UserId}).Find(&orderList)
//	for _, order := range orderList {
//		response.OrderList = append(response.OrderList, &proto.OrderInfoResponse{
//			Id:          order.ID,
//			UserId:      order.UserId,
//			OrderNo:     order.OrderNo,
//			PayType:     order.PayType,
//			OrderStatus: order.OrderStatus,
//			PayPrice:    order.OrderPrice,
//			Address:     order.Address,
//			Username:    order.SingerName,
//			Mobile:      order.SingerPhone,
//			AddTime:     order.CreateAt.Format("2006-01-02 15:04:05"),
//			Post:        order.Post,
//		})
//	}
//
//	return &response, nil
//}
//
//// 订单明细
//func (handler *OrderServer) FindGoodsOrderDetail(ctx context.Context, req *proto.OrderRequest) (*proto.OrderInfoDetailResponse, error) {
//	var order model.OrderInfo
//	var response proto.OrderInfoDetailResponse
//	if result := global.DB.Where(&model.OrderInfo{BaseModel: model.BaseModel{ID: req.Id}, UserId: req.UserId}).First(&order); result.RowsAffected == 0 {
//		return nil, status.Errorf(codes.NotFound, "订单不存在!")
//	}
//
//	orderInfoResponse := proto.OrderInfoResponse{}
//	orderInfoResponse.Id = order.ID
//	orderInfoResponse.UserId = order.ID
//	orderInfoResponse.OrderNo = order.OrderNo
//	orderInfoResponse.PayType = order.PayType
//	orderInfoResponse.OrderStatus = order.OrderStatus
//	orderInfoResponse.PayPrice = order.OrderPrice
//	orderInfoResponse.Address = order.Address
//	orderInfoResponse.Username = order.SingerName
//	orderInfoResponse.Mobile = order.SingerPhone
//	orderInfoResponse.AddTime = order.CreateAt.Format("2006-01-02 15:04:05")
//	orderInfoResponse.Post = order.Post
//	response.OrderInfo = &orderInfoResponse
//
//	// 查询订单明细
//	var orderGoods []model.OrderGoodsInfo
//	if result := global.DB.Where(&model.OrderGoodsInfo{OrderId: order.ID}).Find(&orderGoods); result.Error != nil {
//		return nil, result.Error
//	}
//	for _, orderGoods := range orderGoods {
//		response.GoodsList = append(response.GoodsList, &proto.OrderItemsResponse{
//			Id:         orderGoods.ID,
//			OrderId:    orderGoods.OrderId,
//			GoodsId:    orderGoods.GoodsId,
//			GoodsName:  orderGoods.GoodsName,
//			GoodsImage: orderGoods.GoodsImage,
//			GoodsPrice: orderGoods.GoodsPrice,
//			Nums:       orderGoods.Nums,
//		})
//	}
//
//	return &response, nil
//}
//
//// 订单状态
//func (handler *OrderServer) UpdateGoodsOrderStatus(ctx context.Context, req *proto.OrderStatus) (*emptypb.Empty, error) {
//	//先查询，再更新 实际上有两条sql执行， select 和 update语句
//	if result := global.DB.Model(&model.OrderInfo{}).Where("order_no = ?", req.OrderNo).Update("order_status", req.OrderStatus); result.RowsAffected == 0 {
//		return nil, status.Errorf(codes.NotFound, "订单不存在")
//	}
//	return &emptypb.Empty{}, nil
//}
